package com.xpm.activity;

import android.annotation.SuppressLint;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.xpm.adapter.advance.ItemListViewAdapter;
import com.xpm.base.BaseActivity;
import com.xpm.databinding.ActivityHomeBinding;
import com.xpm.entity.DataProvider;
import com.xpm.entity.ItemBean;

import java.util.ArrayList;
import java.util.List;

public class HomeActivity extends BaseActivity<ActivityHomeBinding> {

    private List<ItemBean> mDatas;
    private ItemListViewAdapter mAdapter;

    @Override
    protected ActivityHomeBinding getBinding() {
        return ActivityHomeBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {
        setTitle("BaseAdapter使用");
        // 设置布局管理器
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setReverseLayout(false);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        binding.rvList.setLayoutManager(layoutManager);

        mAdapter = new ItemListViewAdapter();
        binding.rvList.setAdapter(mAdapter);
    }

    @SuppressLint("NotifyDataSetChanged")
    @Override
    protected void initData() {
        // 获取数据源,一般情况下在网上获取
        mDatas = new ArrayList<>();
        // 创建模拟数据
        for (int i = 0; i < DataProvider.icons.length; i++){
            ItemBean bean = new ItemBean();
            bean.icon =DataProvider.icons[i];
            bean.title = "标题"+i;
            mDatas.add(bean);
        }
        mAdapter.setData(mDatas);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    protected void initListener() {
        // 设置点击监听
        mAdapter.setOnItemClickListener((user, position) -> {
            // 处理点击事件
            Toast.makeText(getApplicationContext(),"点击事件："+position,Toast.LENGTH_SHORT).show();
        });

        // 设置长按监听
        mAdapter.setOnItemLongClickListener((user, position) -> {
            // 处理长按事件
            Toast.makeText(getApplicationContext(),"长按事件："+position,Toast.LENGTH_SHORT).show();
            return true;
        });
    }



}